/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2022-10-13
 * Time: 19:11
 */
public class Test {
    public static MySingleList.ListNode reverseBetween (MySingleList.ListNode head, int m, int n) {
        // write code here
        if (m == n) {
            return head;
        }
        MySingleList.ListNode curPrev = head;
        for (int i = 1; i < m; i++) {
            curPrev = curPrev.next;
        }
        MySingleList.ListNode mNodePrev = head;
        for (int i = 1; i < m-1; i++) {
            mNodePrev = mNodePrev.next;
        }
        MySingleList.ListNode mNode = curPrev;
        MySingleList.ListNode cur = curPrev.next;
        for (int i = 0; i < m-n; i++) {
            MySingleList.ListNode curNext = cur.next;
            cur.next = curPrev;
            curPrev = cur;
            cur = curNext;
        }
        mNodePrev.next = curPrev;
        mNode.next = cur;
        if (mNode == head) {
            head = curPrev;
        }
        return head;
    }
    public static MySingleList.ListNode ReverseList(MySingleList.ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        MySingleList.ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            MySingleList.ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(1);
        mySingleList.addLast(2);
        mySingleList.addLast(3);

    }

}
